package com.example.demo.controller;

import com.example.demo.commom.Result;
import com.example.demo.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@Slf4j
public class FileController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
//    // 使用更规范的路径分隔符
//    private static final String UPLOAD_DIR = "D:/book-upload/";
//    @PostMapping("/upload")
//    public Result upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException, IOException {
//        File folder = new File(UPLOAD_DIR);
//        if (!folder.exists()) folder.mkdirs();
//        log.info("上传文件：{}", file.getOriginalFilename());
//        String filename = UUID.randomUUID() + "_" + file.getOriginalFilename();
//        File destFile = new File(folder, filename);
//        file.transferTo(destFile);
//      //  http://localhost:9090/images/1a2b3c4d_why10k.jpg
//        String url = request.getScheme() + "://" + request.getServerName() + ":" +
//                request.getServerPort() + "/images/" + filename;
//        return Result.success(url);
//    }
    @PostMapping("/upload")
    public Result<?> upload( MultipartFile file, HttpServletRequest request)
            throws IOException {
   log.info("上传文件：{}", file.getOriginalFilename());
    String url=aliOSSUtils.upload(file);
    return Result.success(url);
    }
}